<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>地图拖拽效果</title>
    <style>
        *{
            margin: 0;
            padding:0;
        }
        html,body, #app{
            width: 100vw;
            height: 100vh;
            overflow:hidden;
            position:relative;
        }
        img{
            position:absolute;
            left: 0;
            top: 0;
        }
    </style>
</head>
<body>
    <div id="app">
        <img src="./map.png" alt="">
    </div>

    <script>
        let app = document.querySelector('#app');
        let img = document.querySelector('img');
        app.addEventListener('touchstart', function(e){
            //阻止默认行为
            e.preventDefault();
        });

        //绑定事件
        app.addEventListener('touchstart', function(e){
            //获取手指开始触摸的位置
            this.x = e.changedTouches[0].clientX;
            this.l = img.offsetLeft;

            this.y = e.changedTouches[0].clientY;
            this.t = img.offsetTop;

        })

        app.addEventListener('touchmove', function(e){
            this._x = e.changedTouches[0].clientX;
            this._y = e.changedTouches[0].clientY;
            //计算新的 left 的值
            let newLeft = this._x - this.x + this.l;
            let newTop = this._y - this.y + this.t;
            //设置
            img.style.left = newLeft + 'px';
            img.style.top = newTop + 'px';
        })
    </script>
</body>
</html>